package com.jilian.mydemo.modul.my.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.VideoArticlesDto;
import com.jilian.mydemo.listener.CustomItemClickListener;

import java.util.List;

import cn.jzvd.JzvdStd;


public class ClassRoomAdapter extends RecyclerView.Adapter<ClassRoomAdapter.ViewHolder> {
    private Activity mContext;
    private List<VideoArticlesDto> datas;
    private CustomItemClickListener listener;

    public ClassRoomAdapter(Activity context, List<VideoArticlesDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_class_room, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //1文章，2视频
        if(datas.get(position).getType()==1){
            //显示图片
            Glide
                    .with(mContext)
                    .load(datas.get(position).getPath())
                    .into(holder.ivHead);
            holder.ivVideo.setVisibility(View.GONE);
            holder.ivHead.setVisibility(View.VISIBLE);
        }
        else{

            holder.ivVideo.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.ivVideo.setUp(datas.get(position).getPath(), "",JzvdStd.SCREEN_WINDOW_NORMAL);

            holder.ivVideo.thumbImageView.setImageBitmap(datas.get(position).getBitmap());

            JzvdStd.setJzUserAction(null);
            //
            holder.ivVideo.setVisibility(View.VISIBLE);
            holder.ivHead.setVisibility(View.GONE);


        }
        holder.tvTitle.setText(datas.get(position).getTitle());
        holder.tvContent.setText(datas.get(position).getSummary());
        if("1".equals(datas.get(position).getIsHot())){
            holder.tvIsHot.setVisibility(View.VISIBLE);
        }
        else{
            holder.tvIsHot.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivHead;
        private TextView tvTitle;
        private TextView tvContent;
        private JzvdStd ivVideo;
        private LinearLayout llContent;
        private TextView tvIsHot;







        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            ivVideo = (JzvdStd)itemView. findViewById(R.id.iv_video);
            llContent = (LinearLayout) itemView.findViewById(R.id.ll_content);
            tvIsHot = (TextView) itemView.findViewById(R.id.tv_is_hot);
            llContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView,getAdapterPosition());
                }
            });


        }

    }
}
